{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4bb79796-7bd9-4bb5-8f41-234c9a576f29",
   "metadata": {},
   "source": [
    "# Step 3: Inference"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd01663b-80d2-4ba5-bf6c-17d28a2cd817",
   "metadata": {},
   "source": [
    "## Connecting to Hopsworks Feature Store"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "172f104b-815b-4f53-adba-e07775e72ac6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hopsworks\n",
    "from ipython_secrets import *\n",
    "\n",
    "KEY = get_secret('HOPSWORKS_API_KEY')\n",
    "project = hopsworks.login(host=\"c.app.hopsworks.ai\", api_key_value=KEY)\n",
    "\n",
    "fs = project.get_feature_store()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b0b1e13-dcac-40d6-82c4-430121b78b31",
   "metadata": {},
   "source": [
    "## Feature Retrieval"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ead9268-6ad4-49f6-bbc7-524a297833f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "trans_fg = fs.get_feature_group(\n",
    "    name=\"transactions_fraud_streaming_fg_\" + str(project.id),\n",
    "    version=1,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b13171a8-1287-42e8-a31e-1c7648607029",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Retrieve the first 5 unique credit card numbers (cc_nums)\n",
    "cc_nums = trans_fg.select('cc_num').show(5).cc_num.values\n",
    "\n",
    "# Display the obtained cc_nums\n",
    "cc_nums"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0635e7ce-f482-467d-a850-fd0b42f10b6b",
   "metadata": {},
   "source": [
    "## Model Registry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5139db6-0b47-4ede-a7ef-0997d40bca7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the Model Registry\n",
    "mr = project.get_model_registry()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efa8b687-8e35-4fcc-8c68-01f55bed3485",
   "metadata": {},
   "source": [
    "## Fetch Deployment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8015f80c-b559-44c4-a6cf-a5ab5af7c238",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Access the Model Serving\n",
    "ms = project.get_model_serving()\n",
    "\n",
    "# Specify the deployment name\n",
    "deployment_name = \"fraudonlinemodeldeployment\"\n",
    "\n",
    "# Get the deployment with the specified name\n",
    "deployment = ms.get_deployment(deployment_name)\n",
    "\n",
    "# Start the deployment and wait for it to be in a running state for up to 300 seconds\n",
    "deployment.start(await_running=300)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a936fdc-9bfe-4cf9-9591-7acda557e7ec",
   "metadata": {},
   "source": [
    "## Predict using deployment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb0c192c-db79-4485-8dab-fd08709256f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the first credit card number\n",
    "cc_num = cc_nums[0]\n",
    "cc_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5432e10a-6c9e-4df0-a2e3-0ae156323af5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Make a prediction\n",
    "deployment.predict(\n",
    "    inputs=[cc_num],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df0f0e94-4a53-4a43-8757-985bd21bf0f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Predict for several cc_nums\n",
    "predictions = [\n",
    "    deployment.predict(inputs=[int(cc_num)])['predictions'] \n",
    "    for cc_num\n",
    "    in cc_nums\n",
    "]\n",
    "predictions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8187a805-a9ad-46b6-b411-0952b0d4bf7f",
   "metadata": {},
   "source": [
    "## Stop Deployment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9581843b-1bef-4a42-8592-c857b011dd05",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Stop the deployment\n",
    "deployment.stop(await_stopped=180)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
